package org.dolphin.action.interceptor;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;

public class JSONExceptionInterceptor extends AbstractInterceptor {
  private static final long serialVersionUID = 5093508243060353833L;

  @Override
  public String intercept(ActionInvocation invocation) throws Exception {
    String result = null;
    try {
      result = invocation.invoke();
    } catch (Exception e) {
      ValueStack stack = invocation.getStack();
      String msg = e.getMessage();
      List<String> errors = new ArrayList<String>(1);
      errors.add(msg);
      stack.set("errors", errors);
      stack.set("status", "error");
      result = "json";
    }
    return result;
  }
}
